package com.yssoft.service.special;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yssoft.entity.special.TSCertificationInfo;
import com.yssoft.mapper.special.TSCertificationInfoMapper;
import com.yssoft.utils.UploadFile;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.UUID;


/**
 * @Author: xuzhipeng
 * @Date: 2025/7/31 15:47
 **/
@Service
public class TSCertificationInfoService extends ServiceImpl<TSCertificationInfoMapper, TSCertificationInfo> {

    @Resource
    private TSCertificationInfoMapper tSCertificationInfoMapper;

    public List<TSCertificationInfo> getAll(int pageNum, int pageSize, Long companyId){
        List<TSCertificationInfo> tsCertificationInfos = tSCertificationInfoMapper.getAll(companyId);
        return tsCertificationInfos;
    }

    /**
     * 保存
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean addTSCertificationInfo(TSCertificationInfo entity, MultipartFile typeReportFile, MultipartFile batchReportFile, MultipartFile packagingImgFile){
        entity.setId(UUID.randomUUID().toString().replaceAll("-", ""));

        // 处理在产产品型式检验报告文件上传
        if (typeReportFile != null && !typeReportFile.isEmpty() && !typeReportFile.getOriginalFilename().isEmpty()) {
            try {
                // 使用与人员信息相同的方法，直接返回文件路径
                String filePath = UploadFile.processUploadedFile(typeReportFile);
                entity.setTypeReport(filePath);
                entity.setTypeReportOriginalName(filePath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 处理在产产品的批次检验报告文件上传
        if (batchReportFile != null && !batchReportFile.isEmpty() && !batchReportFile.getOriginalFilename().isEmpty()) {
            try {
                // 使用与人员信息相同的方法，直接返回文件路径
                String filePath = UploadFile.processUploadedFile(batchReportFile);
                entity.setBatchReport(filePath);
                entity.setBatchReportOriginalName(filePath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 处理包装图片文件上传
        if (packagingImgFile != null && !packagingImgFile.isEmpty() && !packagingImgFile.getOriginalFilename().isEmpty()) {
            try {
                // 使用与人员信息相同的方法，直接返回文件路径
                String filePath = UploadFile.processUploadedFile(packagingImgFile);
                entity.setPackagingImg(filePath);
                entity.setPackagingImgOriginalName(filePath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if(this.save(entity)){
            return true;
        }
        return false;
    }

    /**
     * 修改根居ID
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean update(TSCertificationInfo entity, MultipartFile typeReportFile, MultipartFile batchReportFile, MultipartFile packagingImgFile) {
        // 处理在产产品型式检验报告文件上传
        if (typeReportFile != null && !typeReportFile.isEmpty() && !typeReportFile.getOriginalFilename().isEmpty()) {
            try {
                // 使用与人员信息相同的方法，直接返回文件路径
                String filePath = UploadFile.processUploadedFile(typeReportFile);
                entity.setTypeReport(filePath);
                entity.setTypeReportOriginalName(filePath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 处理在产产品的批次检验报告文件上传
        if (batchReportFile != null && !batchReportFile.isEmpty() && !batchReportFile.getOriginalFilename().isEmpty()) {
            try {
                // 使用与人员信息相同的方法，直接返回文件路径
                String filePath = UploadFile.processUploadedFile(batchReportFile);
                entity.setBatchReport(filePath);
                entity.setBatchReportOriginalName(filePath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 处理包装图片文件上传
        if (packagingImgFile != null && !packagingImgFile.isEmpty() && !packagingImgFile.getOriginalFilename().isEmpty()) {
            try {
                // 使用与人员信息相同的方法，直接返回文件路径
                String filePath = UploadFile.processUploadedFile(packagingImgFile);
                entity.setPackagingImg(filePath);
                entity.setPackagingImgOriginalName(filePath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if(this.updateById(entity)){
            return true;
        }
        return false;
    }

    /**
     * 软删除
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(TSCertificationInfo entity) {
        entity.setDeleteFlag(1);
        return this.updateById(entity);
    }

}